package com.sunseen.intelligencebus.user

import android.os.Bundle
import android.os.Handler
import android.view.View
import android.widget.Button
import android.widget.RadioGroup
import android.widget.RelativeLayout
import android.widget.Toast
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.databinding.ViewDataBinding
import com.sunseen.intelligencebus.R
import com.sunseen.intelligencebus.base.AbsBindingFragment
import com.sunseen.intelligencebus.databinding.FragmentTopUpBinding
import com.sunseen.intelligencebus.riding.QRCodeFragment
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class TopUpFragment : AbsBindingFragment() {

    companion object {
        const val BalanceUpdatedStr = "BalanceUpdated"
    }

    override fun setLayout(): Int = R.layout.fragment_top_up

    var topUpValue: Double = 10.0

    var fromQrcode = 0 // 1 标志来自二维码界面的充值请求 ，0 其他的充值请求
    lateinit var topUpBtn: Button
    lateinit var progressView: ConstraintLayout

    override fun onBindView(viewDataBinding: ViewDataBinding, bundle: Bundle?) {
        EventBus.getDefault().register(this)
        fromQrcode = (arguments?.getInt(QRCodeFragment.TopUpStr, 0)) ?: 0
        val binding = viewDataBinding as FragmentTopUpBinding
        progressView = binding.topUpCover
        binding.toolBar.setNavigationOnClickListener {
            pop()
        }
        binding.priceRg.setOnCheckedChangeListener(object : RadioGroup.OnCheckedChangeListener {
            override fun onCheckedChanged(group: RadioGroup?, checkedId: Int) {
                when (checkedId) {
                    R.id.rmb_10_rbtn -> topUpValue = 10.0

                    R.id.rmb_20_rbtn -> topUpValue = 20.0

                    R.id.rmb_30_rbtn -> topUpValue = 30.0

                    R.id.rmb_50_rbtn -> topUpValue = 50.0

                    R.id.rmb_100_rbtn -> topUpValue = 100.0

                    R.id.rmb_200_rbtn -> topUpValue = 200.0
                }
            }
        })

        topUpBtn = binding.topUpBtn
        binding.topUpBtn.setOnClickListener {
            //显示旋转条
            showProgress(true)
            BalanceManager.addBalance(topUpValue)
        }
    }

    private fun showProgress(bool: Boolean) {
        topUpBtn.isEnabled = !bool
        progressView.visibility = if (bool) View.VISIBLE else View.GONE

    }

    override fun onDestroyView() {
        super.onDestroyView()
        EventBus.getDefault().unregister(this)
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onBalanceUpdate(update: BalanceUpdateEvent) {
        Handler().postDelayed(Runnable {
            showProgress(false)
            Toast.makeText(context, "充值成功$fromQrcode", Toast.LENGTH_SHORT).show()
            if (fromQrcode == QRCodeFragment.FromRequestTopUp) {
                val resultBundle = Bundle()
                resultBundle.putDouble(BalanceUpdatedStr, update.newValue)
                setFragmentResult(QRCodeFragment.RequestTopUp, resultBundle)
                pop()
            }
        }, 1500)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onBalanceUpdateFailed(failed: BalanceUpdateFailedEvent) {
        Handler().postDelayed(Runnable {
            showProgress(false)
            Toast.makeText(context, "充值失败", Toast.LENGTH_SHORT).show()
        }, 1500)
    }
}